<?php

#<editor-fold defaultstate="collapsed" desc="CCore::Using">
CCore::Using('socket.inc.streams.inc.interface');
#</editor-fold>

class CCoreSocketIncStreamsWeb implements ICoreSocketIncStreams
{
	#<editor-fold defaultstate="collapsed" desc="private">
	/**
	 * @var	string
	 */
	private $_sStreamData;

	/**
	 * @var	array
	 */
	private $_aCurrentResult;

	/**
	 * @var	int
	 */
	private $_iCurrentResultIndex;

	/**
	 * @var	bool
	 */
	private $_bIsConnected;
	#</editor-fold>

	/**
	 * @param	string	$sStreamData
	 * @return	CCoreSocketIncStreamsSocket
	 */
	public function __construct($sStreamData)
	{
		CCore::CheckTypes(array('$sStreamData'));

		$this->_sStreamData = $sStreamData;
		$this->_aCurrentResult = array();
		$this->_iCurrentResultIndex = 0;
		$this->_bIsConnected = false;
	}

	/**
	 * @param	string	$sHost
	 * @param	int		$iPort
	 * @param	int		&$iErrorNo
	 * @param	string	&$sErrorStr
	 * @param	int		$iConnectTimeOut
	 * @return	bool
	 */
	public function Open($sHost, $iPort, &$iErrorNo, &$sErrorStr, $iConnectTimeOut)
	{
		CCore::CheckTypes(array('$sHost', '$iPort', '$iErrorNo', '$sErrorStr', '$iConnectTimeOut'));

		$this->_bIsConnected = true;
		$this->_iCurrentResultIndex = 0;
		$this->_aCurrentResult = explode("\n", $this->_getResultFromWeb());
		return $this->IsConnected();
	}

	/**
	 * @return	void
	 */
	public function Close()
	{
		$this->_aCurrentResult = array();
		$this->_iCurrentResultIndex = 0;
		$this->_bIsConnected = false;
		return true;
	}

	/**
	 * @return	bool
	 */
	public function IsConnected()
	{
		return $this->_bIsConnected;
	}

	/**
	 * @param	string	$sLine
	 * @return	bool
	 */
	public function Write($sLine)
	{
		CCore::CheckTypes(array('$sLine'));

		$this->_iCurrentResultIndex = 0;
		$this->_aCurrentResult = explode("\n", $this->_getResultFromWeb($sLine));
		return true;
	}

	/**
	 * @return	string | false
	 */
	public function Read()
	{
		if (isset($this->_aCurrentResult[$this->_iCurrentResultIndex]))
		{
			return $this->_aCurrentResult[$this->_iCurrentResultIndex++]."\n";
		}
		return false;
	}

	/**
	 * @return	array | false
	 */
	public function Status()
	{
		return array();
	}

	/**
	 * @param	int		$iSocketTimeOut
	 * @param	bool	$bSocketBlocking
	 */
	public function SetTimeAndBlocking($iSocketTimeOut, $bSocketBlocking)
	{
		CCore::CheckTypes(array('$iSocketTimeOut', '$bSocketBlocking'));
	}

	/**
	 * @param	string	$sLine = ''
	 * @return	string
	 */
	private function _getResultFromWeb($sLine = '')
	{
		CCore::CheckTypes(array('$sLine'));

		$mReturn = false;
		if (strlen($sLine) > 32)
		{
			$aOpts = array(
				'http' => array(
					'method' => 'POST',
					'header' => array (
						'User-Agent: Opera/9.50 (Windows NT 5.1; U; ru)',
						'Accept-Charset: windows-1251, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1',
						'Proxy-Connection: close',
						'Content-Type: application/x-www-form-urlencoded',
						'Content-Length: "'.strlen($sLine).'"'
					),
					'content' => http_build_query(array('webstreams' => '1', 'cmd' => $sLine)),
					'timeout' => 10
				)
			);

			$mReturn = @file_get_contents($this->_sStreamData, false, stream_context_create($aOpts));
		}
		else
		{
			$mReturn = @file_get_contents($this->_sStreamData.'?webstreams=1&cmd='.urlencode($sLine));
		}

		return (false !== $mReturn) ? $mReturn : '';
	}
}